package com.coincalf.admin.service.impl;

import com.baomidou.mybatisplus.mapper.SqlHelper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.coincalf.admin.dto.UserCountLoginDTO;
import com.coincalf.admin.entity.UserLoginLog;
import com.coincalf.admin.mapper.*;
import com.coincalf.admin.service.UserLoginLogService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.coincalf.framework.enums.CashRechargeStatus;
import com.google.common.base.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 用户登录日志 服务实现类
 * </p>
 *
 * @author qiang
 * @since 2018-05-13
 */
@Service
public class UserLoginLogServiceImpl extends ServiceImpl<UserLoginLogMapper, UserLoginLog> implements UserLoginLogService {

    @Autowired
    private UserLoginLogMapper userLoginLogMapper;

    @Autowired
    private TurnoverOrderMapper turnoverOrderMapper;

    @Autowired
    private CashRechargeMapper cashRechargeMapper;

    @Autowired
    private CashWithdrawalsMapper cashWithdrawalsMapper;

    @Autowired
    private CoinRechargeMapper coinRechargeMapper;

    @Autowired
    private CoinWithdrawMapper coinWithdrawMapper;

    @Override
    public Page<UserCountLoginDTO> selectLoginCountPage(Page<UserCountLoginDTO> page, Wrapper<UserCountLoginDTO> wrapper) {
        wrapper = (Wrapper<UserCountLoginDTO>) SqlHelper.fillWrapper(page, wrapper);
        List<UserCountLoginDTO> countRegDTOList = this.userLoginLogMapper.selectLoginCountPage(page, wrapper);
        for (UserCountLoginDTO countDTO:countRegDTOList) {
            //登陆人数
            countDTO.setLoginNum(userLoginLogMapper.countloginNumByDate(countDTO.getLoginDate()));
            String idStr = userLoginLogMapper.selectUserIdStrsBydate(countDTO.getLoginDate());
            if(!Strings.isNullOrEmpty(idStr)){
                //参与交易人数
                countDTO.setTradeNum(turnoverOrderMapper.countTradeByDateAndUidStrs(countDTO.getLoginDate(),idStr));
                //充值人数
                Integer rechargeNum = cashRechargeMapper.countByDateAndUidStrs(
                        countDTO.getLoginDate(),idStr, -1);//-1表示不限状态
                countDTO.setRechargeNum(rechargeNum);
                //提现人数
                Integer withdrawaNum = cashWithdrawalsMapper.countByDateAndUidStrs(
                         countDTO.getLoginDate(),idStr, -1);
                countDTO.setWithdrawNum(withdrawaNum);
                //充币人数
                Integer rechargeCoinNum = coinRechargeMapper.countByDateAndUidStrs(
                        countDTO.getLoginDate(),idStr, -1);
                countDTO.setRechargeCoinNum(rechargeCoinNum);
                //提币人数
                Integer withdrawCoinNum = coinWithdrawMapper.countByDateAndUidStrs(
                        countDTO.getLoginDate(),idStr, -1);
                countDTO.setWithdrawCoinNum(withdrawCoinNum);

            }else{
                countDTO.setRechargeCoinNum(0);
                countDTO.setRechargeNum(0);
                countDTO.setTradeNum(0);
                countDTO.setWithdrawCoinNum(0);
                countDTO.setWithdrawNum(0);
            }
        }
        page.setRecords(countRegDTOList);
        return page;
    }
}
